

#include <material/materialview.h>
#include <stdio.h>
#include <QGraphicsSceneMouseEvent>




/* *********************************************
 *   cceMaterialButtonItem
 * *********************************************/

cceMaterialButtonItem::cceMaterialButtonItem (QGraphicsItem* parent)
	: QGraphicsObject (parent)
{
	_background = new QGraphicsRectItem(this);
	_background->setPen (QColor (0, 0, 0));
  _background->setRect (0, 0, CCE_MATERIAL_BUTTON_SIZE, CCE_MATERIAL_BUTTON_SIZE);

	setAcceptHoverEvents (true);
	setAcceptedMouseButtons (Qt::LeftButton);
}

void cceMaterialButtonItem::SetBrushes (const QBrush &normal, const QBrush &hover)
{
	_brushNormal = normal;
	_brushHover = hover;
	_background->setBrush (normal);
}

void cceMaterialButtonItem::hoverEnterEvent(QGraphicsSceneHoverEvent* evt)
{
	_background->setBrush (_brushHover);
  _background->setCursor(Qt::ArrowCursor);
	QGraphicsItem::hoverEnterEvent (evt);
}

void cceMaterialButtonItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* evt)
{
	_background->setBrush (_brushNormal);
  _background->setCursor(Qt::ArrowCursor);
  QGraphicsItem::hoverLeaveEvent (evt);
}


void cceMaterialButtonItem::mousePressEvent (QGraphicsSceneMouseEvent* evt)
{
	if (evt->button () == Qt::LeftButton) 
		{
			evt->accept ();
		}
}

void cceMaterialButtonItem::mouseReleaseEvent (QGraphicsSceneMouseEvent* evt)
{
	if (evt->button () == Qt::LeftButton &&
		boundingRect ().contains (evt->pos ()))
		{
			emit clicked ();
		}
}

QRectF cceMaterialButtonItem::boundingRect () const
{
  return QRectF (0, 0, CCE_MATERIAL_BUTTON_SIZE, CCE_MATERIAL_BUTTON_SIZE);
}

void cceMaterialButtonItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(painter);
	Q_UNUSED(option);
	Q_UNUSED(widget);
}

/* *********************************************
 *   cceMaterialAddItem
 * *********************************************/

cceMaterialAddItem::cceMaterialAddItem (QGraphicsItem* parent)
	: cceMaterialButtonItem (parent)
{
  SetBrushes (QColor (128, 128, 128), QColor (64, 128, 64));

  float half = (float)CCE_MATERIAL_BUTTON_SIZE / 2.0f - 0.0f;
	float p0 = 2.0f;
  float p1 = (float)CCE_MATERIAL_BUTTON_SIZE - 2.0f;

	_line0 = new QGraphicsLineItem (this);
	_line0->setLine (p0, half, p1, half);

	_line1 = new QGraphicsLineItem (this);
	_line1->setLine (half, p0, half, p1);
}

/* *********************************************
 *   cceMaterialRemoveItem
 * *********************************************/

cceMaterialRemoveItem::cceMaterialRemoveItem (QGraphicsItem* parent)
	: cceMaterialButtonItem (parent)
{
  SetBrushes (QColor (128, 128, 128), QColor (128, 64, 64));

  float half = (float)CCE_MATERIAL_BUTTON_SIZE / 2.0f - 0.0f;
	float p0 = 2.0f;
  float p1 = (float)CCE_MATERIAL_BUTTON_SIZE - 2.0f;

	_line = new QGraphicsLineItem (this);
	_line->setLine (p0, half, p1, half);

}

/* *********************************************
 *   cceMaterialMoveUpItem
 * *********************************************/

cceMaterialMoveUpItem::cceMaterialMoveUpItem (QGraphicsItem* parent)
	: cceMaterialButtonItem (parent)
{
	SetBrushes (QColor (255, 255, 255, 0), QColor (255, 255, 255, 128));

  float half = (float)CCE_MATERIAL_BUTTON_SIZE / 2.0f - 0.0f;
	float p0 = 2.0f;
  float p1 = (float)CCE_MATERIAL_BUTTON_SIZE - 2.0f;


	_path = new QGraphicsPathItem (this);
	QPainterPath path;
	path.moveTo (p0, p1);
	path.lineTo (half, p0);
	path.lineTo (p1, p1);
	path.lineTo (p0, p1);
	_path->setPath (path);
	_path->setBrush (QColor (128, 128, 128));
}

/* *********************************************
 *   cceMaterialMoveDownItem
 * *********************************************/

cceMaterialMoveDownItem::cceMaterialMoveDownItem (QGraphicsItem* parent)
	: cceMaterialButtonItem (parent)
{
	SetBrushes (QColor (255, 255, 255, 0), QColor (255, 255, 255, 128));

  float half = (float)CCE_MATERIAL_BUTTON_SIZE / 2.0f - 0.0f;
	float p0 = 2.0f;
  float p1 = (float)CCE_MATERIAL_BUTTON_SIZE - 2.0f;


	_path = new QGraphicsPathItem (this);
	QPainterPath path;
	path.moveTo (p0, p0);
	path.lineTo (p1, p0);
	path.lineTo (half, p1);
	path.lineTo (p0, p0);
	_path->setPath (path);
	_path->setBrush (QColor (128, 128, 128));
}


/* *********************************************
 *   cceMaterialBaseItem
 * *********************************************/

cceMaterialBaseItem::cceMaterialBaseItem(QGraphicsItem *parent)
  : QGraphicsObject (parent)
  , _minWidth (0)
  , _minHeight (0)
  , _width (0)
  , _height (0)
	, _view (0)
  , _clickable(false)
  , _unifiedSize(true)
{
  _font.setPixelSize(11);
  _font.setBold(false);
  _font.setFamily("Helvetica");

  _textPen = QPen(QColor(0, 0, 0), 0.1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
}

bool cceMaterialBaseItem::Contains(cceMaterialBaseItem *item)
{
  return _children.contains(item);
}

void cceMaterialBaseItem::AddChild (cceMaterialBaseItem* item)
{
	_children.append (item);
	item->SetView (_view);
}

void cceMaterialBaseItem::AddChild (cceMaterialBaseItem* item, int idx)
{
  _children.insert (idx, item);
  item->SetView (_view);
}

void cceMaterialBaseItem::RemoveChild (cceMaterialBaseItem* item)
{
	_children.removeAll (item);
	item->SetView (0);
}


QList<cceMaterialBaseItem*>& cceMaterialBaseItem::GetChildrenList() 
{
  return _children;
}

const QList<cceMaterialBaseItem*>& cceMaterialBaseItem::GetChildren() const
{
  return _children;
}

void cceMaterialBaseItem::SetClickable(bool clickable)
{
  _clickable = clickable;
}

bool cceMaterialBaseItem::IsClickable() const
{
  return _clickable;
}

void cceMaterialBaseItem::SetUnifiedSize(bool unifiedSize)
{
  _unifiedSize = unifiedSize;
}

bool cceMaterialBaseItem::IsUnifiedSize() const
{
  return _unifiedSize;
}

void cceMaterialBaseItem::SetWidth(unsigned width)
{
  if (width <= _minWidth) return;
  _width = width;
}

void cceMaterialBaseItem::SetHeight(unsigned height)
{
  if (height <= _minHeight) return;
  _height = height;
}

unsigned cceMaterialBaseItem::GetWidth() const
{
  return _width;
}

unsigned cceMaterialBaseItem::GetHeight() const
{
  return _height;
}

unsigned cceMaterialBaseItem::GetMinWidth() const
{
  return _minWidth;
}

unsigned cceMaterialBaseItem::GetMinHeight() const
{
  return _minHeight;
}

QFont cceMaterialBaseItem::GetFont() const
{
  return _font;
}

QPen cceMaterialBaseItem::GetTextPen() const
{
  return _textPen;
}

void cceMaterialBaseItem::SetView (cceMaterialView* view)
{
	_view = view;
	if (!_view && scene ())
		{
			scene ()->removeItem (this);
		}
	else if (_view && _view->scene () != scene ())
		{
			if (scene ())
				{
					scene ()->removeItem (this);
				}
			_view->scene ()->addItem (this);
		}


	foreach (cceMaterialBaseItem* item, _children)
		{
			item->SetView (view);
		}
}

cceMaterialView* cceMaterialBaseItem::GetView () const
{
	return _view;
}

QRectF cceMaterialBaseItem::boundingRect() const
{
  return QRectF (0, 0, _width, _height);
}

void cceMaterialBaseItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
  Q_UNUSED(painter);
  Q_UNUSED(option);
  Q_UNUSED(widget);
}

/* *********************************************
 *   cceMaterialPrimaryItem
 * *********************************************/

cceMaterialPrimaryItem::cceMaterialPrimaryItem (QGraphicsItem* parent, bool addItem)
  : cceMaterialBaseItem (parent)
  , _addItem(0)
{
	_background = new QGraphicsRectItem (this);
	_background->setPen (QColor(0, 0, 0));
  _background->setBrush (QColor (200, 200, 200));

	_separatorItem = new QGraphicsLineItem (this);
	_separatorItem->setPen (QColor (0, 0, 0));

	_typeItem = new QGraphicsSimpleTextItem (this);
  _typeItem->setPen (GetTextPen());
  _typeItem->setFont(GetFont());

	_nameItem = new QGraphicsSimpleTextItem (this);
  _nameItem->setPen (GetTextPen());
  _nameItem->setFont(GetFont());

  if (addItem)
    {
      _addItem = new cceMaterialAddItem (this);
      connect (_addItem, SIGNAL(clicked ()), this, SLOT(AddTechnique ()));
    }

}

void cceMaterialPrimaryItem::SetType (const QString& type)
{
	_typeItem->setText (type);
}

void cceMaterialPrimaryItem::SetName (const QString& name)
{
	_nameItem->setText (name);
}



void cceMaterialPrimaryItem::ComputeSize()
{
  QFontMetrics fm (_typeItem->font ());

  unsigned typeWidth = fm.width (_typeItem->text ());
  unsigned nameWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();

  unsigned width = typeWidth > nameWidth ? typeWidth : nameWidth;

  _minWidth = width + 2*CCE_MATERIAL_ITEM_HBORDER + 2*CCE_MATERIAL_BUTTON_SIZE;
  _minHeight = textHeight * 2 + 4 * CCE_MATERIAL_ITEM_VBORDER;

  _width = _minWidth;
  _height = _minHeight;
}

void cceMaterialPrimaryItem::UpdateLayout ()
{
	QFontMetrics fm (_typeItem->font ());

	unsigned typeWidth = fm.width (_typeItem->text ());
	unsigned nameWidth = fm.width (_nameItem->text ());
	unsigned textHeight = fm.height ();

  unsigned hheight = _height / 2;


  _typeItem->setPos ((_width - typeWidth) / 2.0f, (hheight - textHeight) / 2.0f);
  _nameItem->setPos ((_width - nameWidth) / 2.0f, (hheight - textHeight) / 2.0f + hheight);

  _separatorItem->setLine (CCE_MATERIAL_ITEM_HBORDER, _height / 2, _width - CCE_MATERIAL_ITEM_HBORDER, _height / 2);
  _background->setRect (0, 0, _width, _height);

  if (_addItem)
    {
      _addItem->setPos (_width - CCE_MATERIAL_BUTTON_SIZE, 0);
    }
}


/* *********************************************
 *   cceMaterialTechniqueItem
 * *********************************************/

cceMaterialTechniqueItem::cceMaterialTechniqueItem (bool upItem, bool downItem, bool removeItem)
  : cceMaterialBaseItem ()
  , _upItem (0)
  , _downItem(0)
  , _removeItem(0)
{
  _background = new QGraphicsRectItem (this);
  _background->setPen (QColor(0, 0, 0));
  _background->setBrush (QColor (200, 200, 200));

  _nameItem = new QGraphicsSimpleTextItem (this);
  _nameItem->setPen (GetTextPen());
  _nameItem->setFont(GetFont());

  if (upItem)
    {
      _upItem = new cceMaterialMoveUpItem (this);
      connect (_upItem, SIGNAL(clicked()), this, SLOT(MoveUp ()));
    }

  if (downItem)
    {
      _downItem = new cceMaterialMoveDownItem (this);
      connect (_downItem, SIGNAL(clicked()), this, SLOT(MoveDown ()));
    }

  if (removeItem)
    {
    _removeItem = new cceMaterialRemoveItem (this);
    connect (_removeItem, SIGNAL(clicked()), this, SLOT(RemoveTechnique ()));
    }
}

void cceMaterialTechniqueItem::SetName (const QString& name)
{
  _nameItem->setText (name);
}

void cceMaterialTechniqueItem::ComputeSize()
{
  QFontMetrics fm (_nameItem->font ());
  unsigned textWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();

  _minWidth = textWidth + 2*CCE_MATERIAL_ITEM_HBORDER + 2*CCE_MATERIAL_BUTTON_SIZE;

  int buttonCount = 1;
  if (_upItem) buttonCount++;
  if (_downItem) buttonCount++;

  _minHeight = textHeight + 2 * CCE_MATERIAL_ITEM_VBORDER;
  _minHeight = _minHeight < buttonCount * CCE_MATERIAL_BUTTON_SIZE ? buttonCount * CCE_MATERIAL_BUTTON_SIZE : _minHeight;


  _width = _minWidth;
  _height = _minHeight;
}

void cceMaterialTechniqueItem::UpdateLayout ()
{
  QFontMetrics fm (_nameItem->font ());

  unsigned textWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();


  _nameItem->setPos ((_width - textWidth) / 2.0f, (_height - textHeight) / 2.0f);

  if (_upItem)
    {
      _upItem->setPos(0, 0);
    }

  if (_downItem)
    {
      _downItem->setPos(0, _height - CCE_MATERIAL_BUTTON_SIZE);
    }

  if (_removeItem)
    {
      _removeItem->setPos(_width - CCE_MATERIAL_BUTTON_SIZE, 0);
    }

  _background->setRect (0, 0, _width, _height);
}



/* *********************************************
 *   cceMaterialValueItem
 * *********************************************/

cceMaterialValueItem::cceMaterialValueItem (QGraphicsItem* parent)
  : cceMaterialBaseItem (parent)
{
  setAcceptHoverEvents (true);
  setAcceptedMouseButtons (Qt::LeftButton);
  _background = new QGraphicsRectItem (this);
  SetBackgroundColor(QColor (200, 200, 200));

  _separatorItem = new QGraphicsLineItem (this);
  _separatorItem->setPen (QColor (0, 0, 0));

  _typeItem = new QGraphicsSimpleTextItem (this);
  _typeItem->setPen (GetTextPen());
  _typeItem->setFont(GetFont());

  _nameItem = new QGraphicsSimpleTextItem (this);
  _nameItem->setPen (GetTextPen());
  _nameItem->setFont(GetFont());

  _removeItem = new cceMaterialRemoveItem (this);


	connect (_removeItem, SIGNAL(clicked ()), this, SLOT (RemoveValue ()));
}

void cceMaterialValueItem::hoverEnterEvent(QGraphicsSceneHoverEvent *evt)
{
  _background->setBrush(_backgroundColorHover);
  _background->setCursor(Qt::PointingHandCursor);
}

void cceMaterialValueItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *evt)
{
  _background->setBrush(_backgroundColor);
  _background->setCursor(Qt::ArrowCursor);
}

void cceMaterialValueItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  grabMouse();
}

void cceMaterialValueItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  ungrabMouse();
  if (_background->rect().contains(event->pos()))
    {
      emit Clicked ();
    }
}

void cceMaterialValueItem::SetType (const QString& type)
{
  _typeItem->setText (type);
}

void cceMaterialValueItem::SetName (const QString& name)
{
  _nameItem->setText (name);
}

void cceMaterialValueItem::SetBackgroundColor(const QColor &color)
{
  setAcceptHoverEvents (true);
  setAcceptedMouseButtons (Qt::LeftButton);

  _backgroundColor = color;
  _backgroundColorHover = color;
  _backgroundColorHover.setHsvF(
        color.hueF(),
        color.saturationF(),
        color.valueF () * 0.8f
        );

  _background->setPen(QColor (0, 0, 0));
  _background->setBrush(color);
}

void cceMaterialValueItem::ComputeSize()
{
  QFontMetrics fm (_typeItem->font ());

  unsigned typeWidth = fm.width (_typeItem->text ());
  unsigned nameWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();

  unsigned width = typeWidth > nameWidth ? typeWidth : nameWidth;

  _minWidth = width + 2*CCE_MATERIAL_ITEM_HBORDER + 2*CCE_MATERIAL_BUTTON_SIZE;
  _minHeight = textHeight * 2 + 4 * CCE_MATERIAL_ITEM_VBORDER;

  _width = _minWidth;
  _height = _minHeight;
}

void cceMaterialValueItem::UpdateLayout ()
{
  QFontMetrics fm (_typeItem->font ());

  unsigned typeWidth = fm.width (_typeItem->text ());
  unsigned nameWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();

  unsigned hheight = _height / 2;


  _typeItem->setPos ((_width - typeWidth) / 2.0f, (hheight - textHeight) / 2.0f);
  _nameItem->setPos ((_width - nameWidth) / 2.0f, (hheight - textHeight) / 2.0f + hheight);

  _separatorItem->setLine (CCE_MATERIAL_ITEM_HBORDER, _height / 2, _width - CCE_MATERIAL_ITEM_HBORDER, _height / 2);
  _background->setRect (0, 0, _width, _height);

  _removeItem->setPos (_width - CCE_MATERIAL_BUTTON_SIZE, 0);
}



/* *********************************************
 *   cceMaterialSimpleValueItem
 * *********************************************/

cceMaterialSimpleValueItem::cceMaterialSimpleValueItem (QGraphicsItem* parent)
  : cceMaterialBaseItem (parent)
{
  setAcceptHoverEvents (true);
  setAcceptedMouseButtons (Qt::LeftButton);
  _background = new QGraphicsRectItem (this);
  _background->setPen (QColor(0, 0, 0));
  _background->setBrush (QColor (200, 200, 200));

  _nameItem = new QGraphicsSimpleTextItem (this);
  _nameItem->setPen (GetTextPen());
  _nameItem->setFont(GetFont());
}

void cceMaterialSimpleValueItem::hoverEnterEvent(QGraphicsSceneHoverEvent *evt)
{
  _background->setCursor(Qt::PointingHandCursor);
}

void cceMaterialSimpleValueItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *evt)
{
  _background->setCursor(Qt::ArrowCursor);
}


void cceMaterialSimpleValueItem::SetName (const QString& name)
{
  _nameItem->setText (name);
}

void cceMaterialSimpleValueItem::SetBackgroundColor(const QColor &color)
{
  _background->setPen(QColor (0, 0, 0));
  _background->setBrush(color);
}

void cceMaterialSimpleValueItem::SetTextColor(const QColor &color)
{
  _nameItem->setPen(color);
  _nameItem->setFont(GetFont());
}


void cceMaterialSimpleValueItem::ComputeSize()
{
  QFontMetrics fm (_nameItem->font ());
  unsigned textWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();

  _minWidth = textWidth + 2*CCE_MATERIAL_ITEM_HBORDER;
  _minHeight = textHeight + 2 * CCE_MATERIAL_ITEM_VBORDER;
  _width = _minWidth;
  _height = _minHeight;
}

void cceMaterialSimpleValueItem::UpdateLayout ()
{
  QFontMetrics fm (_nameItem->font ());

  unsigned textWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();


  _nameItem->setPos ((_width - textWidth) / 2.0f, (_height - textHeight) / 2.0f);
  _background->setRect (0, 0, _width, _height);
}


/* *********************************************
 *   cceMaterialSlotItem
 * *********************************************/

cceMaterialSlotItem::cceMaterialSlotItem (QGraphicsItem* parent)
  : cceMaterialBaseItem (parent)
{
	setAcceptDrops (true);

  _background = new QGraphicsRectItem (this);
  _background->setPen (QColor(0, 0, 0));
  _background->setBrush (QColor (255, 255, 255));

  _nameItem = new QGraphicsSimpleTextItem (this);
  _nameItem->setPen (GetTextPen());
  _nameItem->setFont(GetFont());
}

void cceMaterialSlotItem::SetName (const QString& name)
{
  _nameItem->setText (name);
}

void cceMaterialSlotItem::ComputeSize()
{
  QFontMetrics fm (_nameItem->font ());
  unsigned textWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();

  _minWidth = textWidth + 2*CCE_MATERIAL_ITEM_HBORDER;
  _minHeight = textHeight + 2 * CCE_MATERIAL_ITEM_VBORDER;
  _width = _minWidth;
  _height = _minHeight;
}

void cceMaterialSlotItem::UpdateLayout ()
{
  QFontMetrics fm (_nameItem->font ());

  unsigned textWidth = fm.width (_nameItem->text ());
  unsigned textHeight = fm.height ();


  _nameItem->setPos ((_width - textWidth) / 2.0f, (_height - textHeight) / 2.0f);
  _background->setRect (0, 0, _width, _height);
}


/* *********************************************
 *   cceMaterialView
 * *********************************************/

cceMaterialView::cceMaterialView (QWidget* parent)
	: QGraphicsView (parent)
	, _primaryItem (0)
{
	QGraphicsScene* scene = new QGraphicsScene ();
	setScene (scene);
	/*

  _primaryItem = new cceMaterialPrimaryItem ();
  _primaryItem->SetType ("<<Program>>");
  _primaryItem->SetName ("Phong");


  {
    cceMaterialTechniqueItem* technique = new cceMaterialTechniqueItem ();
    technique->SetName("<<Technique>>");
    _primaryItem->GetChildren().append(technique);



    cceMaterialValueItem* shader0 = new cceMaterialValueItem ();
    shader0->SetType("<<VertexShader>>");
    shader0->SetName("org.shaders.standard.Phong.vs");
    technique->GetChildren().append(shader0);


    cceMaterialValueItem* shader1 = new cceMaterialValueItem ();
    shader1->SetType("<<FragmentShader>>");
    shader1->SetName("org.shaders.standard.Phong.fs");
    technique->GetChildren().append(shader1);

    cceMaterialValueItem* shader2 = new cceMaterialValueItem ();
    shader2->SetType("<<FragmentShader>>");
    shader2->SetName("Lighting.vs");
    technique->GetChildren().append(shader2);

    cceMaterialSlotItem* attachShader = new cceMaterialSlotItem ();
    attachShader->SetName("<<Attach Shader>>");
    technique->GetChildren().append(attachShader);
  }

  {
    cceMaterialTechniqueItem* technique = new cceMaterialTechniqueItem ();
    technique->SetName("<<Technique>>");
    _primaryItem->GetChildren().append(technique);



    cceMaterialValueItem* shader0 = new cceMaterialValueItem ();
    shader0->SetType("<<VertexShader>>");
    shader0->SetName("org.shaders.standard.Blinn.vs");
    technique->GetChildren().append(shader0);


    cceMaterialValueItem* shader1 = new cceMaterialValueItem ();
    shader1->SetType("<<FragmentShader>>");
    shader1->SetName("org.shaders.standard.Blinn.fs");
    technique->GetChildren().append(shader1);

    cceMaterialValueItem* shader2 = new cceMaterialValueItem ();
    shader2->SetType("<<FragmentShader>>");
    shader2->SetName("Lighting.vs");
    technique->GetChildren().append(shader2);

    cceMaterialSlotItem* attachShader = new cceMaterialSlotItem ();
    attachShader->SetName("<<Attach Shader>>");
    technique->GetChildren().append(attachShader);
  }
	*/
  UpdateLayout();
}

void cceMaterialView::SetItem (cceMaterialBaseItem* item)
{
	_primaryItem = item;
	_primaryItem->SetView (this);
//	scene()->addItem(_primaryItem);
	UpdateLayout ();
}

void cceMaterialView::UpdateLayout()
{
	if (!_primaryItem)
		{
			return;
		}
  _primaryItem->ComputeSize();

  int prim_width = _primaryItem->GetWidth();
  int technique_width = 0;
  int value_width = 0;
  int max_value_width = 0;


  foreach (cceMaterialBaseItem* technique, _primaryItem->GetChildren())
    {
      technique->ComputeSize();

      technique_width = technique_width < technique->GetWidth() ? technique->GetWidth() : technique_width;

      foreach (cceMaterialBaseItem* value, technique->GetChildren())
        {
          value->ComputeSize();

          if (value->IsUnifiedSize())
            {
              value_width = value_width < value->GetWidth() ? value->GetWidth() : value_width;
            }
          max_value_width = max_value_width < value->GetWidth() ? value->GetWidth() : max_value_width;
        }
    }

  int technique_pos_x = prim_width + 60;
  int technique_pos_y = 0;

  int value_pos_x = technique_pos_x + technique_width;
  int value_pos_y = 0;


  int technique_spacing = 30;
  int value_spacing = 3;

  foreach (cceMaterialBaseItem* technique, _primaryItem->GetChildren())
    {

      int value_height = 0;
      foreach (cceMaterialBaseItem* value, technique->GetChildren())
        {
          if (value->IsUnifiedSize())
            {
              value->SetWidth(value_width);
            }
          value->UpdateLayout();
          value->setPos(value_pos_x, value_pos_y);

          value_height += value->GetHeight() + value_spacing;
          value_pos_y += value->GetHeight() + value_spacing;
        }
			if (value_height < (int)technique->GetHeight () + value_spacing)
				{
					value_height = technique->GetHeight() + value_spacing;
				}

      technique->SetWidth(technique_width);
      technique->UpdateLayout();
      technique->setPos(technique_pos_x, technique_pos_y + (value_height - value_spacing - technique->GetHeight()) / 2.0f);

      technique_pos_y += value_height - value_spacing + technique_spacing;
      value_pos_y = technique_pos_y;

    }
  int height = technique_pos_y - technique_spacing;
	if (height < (int)_primaryItem->GetHeight ())
		{
			height = _primaryItem->GetHeight ();
		}

  _primaryItem->UpdateLayout();
  _primaryItem->setPos(0, (height - _primaryItem->GetHeight()) / 2.0f);

  setSceneRect (0.0f, 0.0f, (float)value_pos_x + (float)max_value_width, (float)height);
  float centerX = (value_pos_x + max_value_width) / 2.0f;
	float centerY = (float)height / 2.0f;
//	centerOn (centerX, centerY);
}
